﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using TTEngine.Properties;

namespace TTEngine
{
    public class Painting : MapObject
    {
        //constructor
        public Painting()
            : base()
        {
            CanTake = true;
            UnitWeight = 2;
            Flamability = 25;
            BurnLifetime = Globals.RandomIntFromTo(5, 8);
            LootLevel = 1;
            ImagesAreUnique = true;
            CanCollapse = false;
            CanReady = true;
            HandsRequired = 1;
            Damage = new DamageExpressionList("1z6=b");
        }

        //clone
        public override MapObject Clone()
        {
            return new Painting();
        }

        //dynamically create a bitmap
        public override Bitmap Image
        {
            get
            {
                //load base images
                if (_basePaintingImages.Count == 0)
                {
                    int n = 1;
                    while (true)
                    {
                        ResourceManager rm = Resources.ResourceManager;
                        Bitmap bmp = (Bitmap)rm.GetObject("Painting" + n);
                        if (bmp == null)
                            break;
                        _basePaintingImages.Add(bmp);
                        n++;
                    }
                }

                if (_myBitmap != null)
                    return _myBitmap;

                //determine size, weight, value
                Bitmap bmpBase = _basePaintingImages.RandomValue;
                _width = Globals.RandomIntFromTo(30, 50);
                _height = Globals.RandomIntFromTo(30, 50);
                UnitWeight = 2.0f * ((_width * _height) / 1000.0f);

                //create a painting image
                int baseWidth = Globals.RandomIntFromTo(_width, 200);
                int baseHeight = Globals.RandomIntFromTo(_height, 200);
                Bitmap chunk = new Bitmap(baseWidth, baseHeight);
                int offX = Globals.RandomIntFromTo(0, bmpBase.Width - baseWidth);
                int offY = Globals.RandomIntFromTo(0, bmpBase.Height - baseHeight);
                if (offX < 0)
                    offX = 0;
                if (offY < 0)
                    offY = 0;
                Graphics g = Graphics.FromImage(chunk);
                using(g)
                {
                    g.DrawImage(bmpBase, new RectangleF(0, 0, baseWidth, baseHeight), new RectangleF(offX, offY, baseWidth, baseHeight), GraphicsUnit.Pixel);
                }
                _myBitmap = new Bitmap(_width, _height);
                g = Graphics.FromImage(_myBitmap);
                using (g)
                {
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
                    g.DrawImage(chunk, new RectangleF(0, 0, _width, _height), new RectangleF(0, 0, baseWidth, baseHeight), GraphicsUnit.Pixel);

                    //frame it
                    Color frameColor = Globals.RandomColor(228, 248, 204, 224, 86, 106);
                    Pen p = new Pen(frameColor);
                    p.Width = 7;
                    g.DrawRectangle(p, 3, 3, _width - 6, _height - 6);
                    p = new Pen(Color.Black);
                    g.DrawRectangle(p, 0, 0, _width - 1, _height - 1);
                    g.DrawRectangle(p, 6, 6, _width - 8 - 5, _height - 8 - 5);
                }

                return _myBitmap;
            }
        }

        //might break
        public override bool BreaksWhenStressed(StressType st)
        {
            return Globals.RandomIntFromTo(1, 100) > 50;
        }

        //private members
        private static RandomList<Bitmap> _basePaintingImages = new RandomList<Bitmap>();
        private Bitmap _myBitmap = null;
        private int _width;
        private int _height;
    }
}
